home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / OpenGL 1.0 SDK / Source / Libraries / aux / aux.c next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  6.9 KB  |  379 lines  |  [TEXT/CWIE]

  1. #include "tk.h"
  2. #include "aux.h"
  3.  
  4. #define AUX_MAX_KEY_CNT     200
  5. #define AUX_MAX_MOUSE_CNT   20
  6.  
  7. static struct {
  8.     int keyField;
  9.     void (*KeyFunc)(void);
  10. } keyTable[AUX_MAX_KEY_CNT];
  11.  
  12. static struct {
  13.     int mouseField;
  14.     void (*MouseFunc)(AUX_EVENTREC *);
  15. } mouseDownTable[AUX_MAX_MOUSE_CNT], mouseUpTable[AUX_MAX_MOUSE_CNT], mouseLocTable[AUX_MAX_MOUSE_CNT];
  16.  
  17. static int keyTableCount        = 0;
  18. static int mouseDownTableCount  = 0;
  19. static int mouseUpTableCount    = 0;
  20. static int mouseLocTableCount   = 0;
  21. static GLenum displayModeType   = 0;
  22. static GLenum displayModePolicy = AUX_MINIMUM_CRITERIA;
  23. static int displayModeID        = 0;
  24.  
  25.  
  26. static void DefaultHandleReshape(int w, int h)
  27. {
  28.     glViewport(0, 0, w, h);
  29.     glMatrixMode(GL_PROJECTION);
  30.     glLoadIdentity();
  31.     glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0);
  32.     glMatrixMode(GL_MODELVIEW);
  33.     glLoadIdentity();
  34. }
  35.  
  36. static void DefaultHandleExpose(int w, int h)
  37. {
  38. }
  39.  
  40. static GLenum MouseLoc(int x, int y, GLenum button)
  41. {
  42.     AUX_EVENTREC info;
  43.     GLenum flag;
  44.     int i;
  45.  
  46.     flag = GL_FALSE;
  47.     
  48.     for(i = 0; i < mouseLocTableCount; i++)
  49.     {
  50.         if((button & AUX_LEFTBUTTON) == mouseLocTable[i].mouseField)
  51.         {
  52.             info.event = AUX_MOUSELOC;
  53.             info.data[AUX_MOUSEX] = x;
  54.             info.data[AUX_MOUSEY] = y;
  55.             info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
  56.             
  57.             (*mouseLocTable[i].MouseFunc)(&info);
  58.             
  59.             flag |= GL_TRUE;
  60.         }
  61.         
  62.         if((button & AUX_RIGHTBUTTON) == mouseLocTable[i].mouseField)
  63.         {
  64.             info.event = AUX_MOUSELOC;
  65.             info.data[AUX_MOUSEX] = x;
  66.             info.data[AUX_MOUSEY] = y;
  67.             info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
  68.             
  69.             (*mouseLocTable[i].MouseFunc)(&info);
  70.             
  71.             flag |= GL_TRUE;
  72.         }
  73.         
  74.         if((button & AUX_MIDDLEBUTTON) == mouseLocTable[i].mouseField)
  75.         {
  76.             info.event = AUX_MOUSELOC;
  77.             info.data[AUX_MOUSEX] = x;
  78.             info.data[AUX_MOUSEY] = y;
  79.             info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
  80.             
  81.             (*mouseLocTable[i].MouseFunc)(&info);
  82.             
  83.             flag |= GL_TRUE;
  84.         }
  85.     }
  86.     
  87.     return flag;
  88. }
  89.  
  90. static GLenum MouseUp(int x, int y, GLenum button)
  91. {
  92.     AUX_EVENTREC info;
  93.     GLenum flag;
  94.     int i;
  95.  
  96.     flag = GL_FALSE;
  97.     
  98.     for(i = 0; i < mouseUpTableCount; i++)
  99.     {
  100.         if((button & AUX_LEFTBUTTON) == mouseUpTable[i].mouseField)
  101.         {
  102.             info.event = AUX_MOUSEUP;
  103.             info.data[AUX_MOUSEX] = x;
  104.             info.data[AUX_MOUSEY] = y;
  105.             info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
  106.             
  107.             (*mouseUpTable[i].MouseFunc)(&info);
  108.             
  109.             flag |= GL_TRUE;
  110.         }
  111.         
  112.         if((button & AUX_RIGHTBUTTON) == mouseUpTable[i].mouseField)
  113.         {
  114.             info.event = AUX_MOUSEUP;
  115.             info.data[AUX_MOUSEX] = x;
  116.             info.data[AUX_MOUSEY] = y;
  117.             info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
  118.             
  119.             (*mouseUpTable[i].MouseFunc)(&info);
  120.             
  121.             flag |= GL_TRUE;
  122.         }
  123.         
  124.         if((button & AUX_MIDDLEBUTTON) == mouseUpTable[i].mouseField)
  125.         {
  126.             info.event = AUX_MOUSEUP;
  127.             info.data[AUX_MOUSEX] = x;
  128.             info.data[AUX_MOUSEY] = y;
  129.             info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
  130.             
  131.             (*mouseUpTable[i].MouseFunc)(&info);
  132.             
  133.             flag |= GL_TRUE;
  134.         }
  135.     }
  136.     
  137.     return flag;
  138. }
  139.  
  140. static GLenum MouseDown(int x, int y, GLenum button)
  141. {
  142.     AUX_EVENTREC info;
  143.     GLenum flag;
  144.     int i;
  145.  
  146.     flag = GL_FALSE;
  147.     
  148.     for(i = 0; i < mouseDownTableCount; i++)
  149.     {
  150.         if((button & AUX_LEFTBUTTON) == mouseDownTable[i].mouseField)
  151.         {
  152.             info.event = AUX_MOUSEDOWN;
  153.             info.data[AUX_MOUSEX] = x;
  154.             info.data[AUX_MOUSEY] = y;
  155.             info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
  156.             
  157.             (*mouseDownTable[i].MouseFunc)(&info);
  158.             
  159.             flag |= GL_TRUE;
  160.         }
  161.         
  162.         if((button & AUX_RIGHTBUTTON) == mouseDownTable[i].mouseField)
  163.         {
  164.             info.event = AUX_MOUSEDOWN;
  165.             info.data[AUX_MOUSEX] = x;
  166.             info.data[AUX_MOUSEY] = y;
  167.             info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
  168.             
  169.             (*mouseDownTable[i].MouseFunc)(&info);
  170.             
  171.             flag |= GL_TRUE;
  172.         }
  173.         
  174.         if((button & AUX_MIDDLEBUTTON) == mouseDownTable[i].mouseField)
  175.         {
  176.             info.event = AUX_MOUSEDOWN;
  177.             info.data[AUX_MOUSEX] = x;
  178.             info.data[AUX_MOUSEY] = y;
  179.             info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
  180.             
  181.             (*mouseDownTable[i].MouseFunc)(&info);
  182.             
  183.             flag |= GL_TRUE;
  184.         }
  185.     }
  186.     
  187.     return flag;
  188. }
  189.  
  190. static GLenum KeyDown(int key, GLenum status)
  191. {
  192.     GLenum flag;
  193.     int i;
  194.  
  195.     flag = GL_FALSE;
  196.     
  197.     if(keyTableCount)
  198.     {
  199.         for(i = 0; i < keyTableCount; i++)
  200.         {
  201.             if(key == keyTable[i].keyField)
  202.             {
  203.                 (*keyTable[i].KeyFunc)();
  204.                 flag = GL_TRUE;
  205.             }
  206.         }
  207.     }
  208.     
  209.     return flag;
  210. }
  211.  
  212. void auxExposeFunc(void (*Func)(int, int))
  213. {
  214.     tkExposeFunc(Func);
  215. }
  216.  
  217. void auxReshapeFunc(void (*Func)(int,int))
  218. {
  219.     tkExposeFunc(Func);
  220.     tkReshapeFunc(Func);
  221. }
  222.  
  223. void auxIdleFunc(void (*Func)(void))
  224. {
  225.     tkIdleFunc(Func);
  226. }
  227.  
  228. void auxKeyFunc(int key, void (*Func)(void))
  229. {
  230.     if(keyTableCount >= AUX_MAX_KEY_CNT) return;
  231.     
  232.     keyTable[keyTableCount].keyField = key;
  233.     keyTable[keyTableCount].KeyFunc  = Func;
  234.     
  235.     keyTableCount++;
  236. }
  237.  
  238. void auxMouseFunc(int mouse, int mode, void (*Func)(AUX_EVENTREC *))
  239. {
  240.     if(mouseLocTableCount >= AUX_MAX_MOUSE_CNT) return;
  241.     
  242.     if(mode == AUX_MOUSEDOWN)
  243.     {
  244.         mouseDownTable[mouseDownTableCount].mouseField = mouse;
  245.         mouseDownTable[mouseDownTableCount].MouseFunc  = Func;
  246.         
  247.         mouseDownTableCount++;
  248.     }
  249.     else if(mode == AUX_MOUSEUP)
  250.     {
  251.         mouseUpTable[mouseUpTableCount].mouseField = mouse;
  252.         mouseUpTable[mouseUpTableCount].MouseFunc  = Func;
  253.         
  254.         mouseUpTableCount++;
  255.     }
  256.     else if(mode == AUX_MOUSELOC)
  257.     {
  258.         mouseLocTable[mouseLocTableCount].mouseField = mouse;
  259.         mouseLocTable[mouseLocTableCount].MouseFunc  = Func;
  260.         
  261.         mouseLocTableCount++;
  262.     } 
  263. }
  264.  
  265. void auxMainLoop(void (*Func)(void))
  266. {
  267.     tkDisplayFunc(Func);
  268.     tkExec();
  269. }
  270.  
  271. void auxInitPosition(int x, int y, int width, int height)
  272. {
  273.     tkInitPosition(x, y, width, height);
  274. }
  275.  
  276. void auxInitDisplayMode(GLenum type)
  277. {
  278.     displayModeType = type;
  279.     tkInitDisplayMode(type);
  280. }
  281.  
  282. void auxInitDisplayModePolicy(GLenum type)
  283. {
  284.     displayModePolicy = type;
  285. }
  286.  
  287. GLenum auxInitDisplayModeID(GLint id)
  288. {
  289.     displayModeID = id;
  290.  
  291.     return 0;
  292. }
  293.  
  294. GLenum auxInitWindow(char *title)
  295. {
  296.     if(tkInitWindow(title) == GL_FALSE) return GL_FALSE;
  297.     
  298.     tkReshapeFunc(DefaultHandleReshape);
  299.     tkExposeFunc(DefaultHandleExpose);
  300.     tkMouseUpFunc(MouseUp);
  301.     tkMouseDownFunc(MouseDown);
  302.     tkMouseMoveFunc(MouseLoc);
  303.     tkKeyDownFunc(KeyDown);
  304.     
  305.     auxKeyFunc(AUX_ESCAPE, auxQuit);
  306.     
  307.     return GL_TRUE;
  308. }
  309.  
  310. void auxCloseWindow(void)
  311. {
  312.     tkCloseWindow();
  313.     
  314.     keyTableCount       = 0;
  315.     mouseDownTableCount = 0;
  316.     mouseUpTableCount   = 0;
  317.     mouseLocTableCount  = 0;
  318. }
  319.  
  320. void auxQuit(void)
  321. {
  322.     tkQuit();
  323. }
  324.  
  325. void auxSwapBuffers(void)
  326. {
  327.     tkSwapBuffers();
  328. }
  329.  
  330. GLenum auxGetDisplayModePolicy(void)
  331. {
  332.    return displayModePolicy;
  333. }
  334.  
  335. GLint auxGetDisplayModeID(void)
  336. {
  337.    return displayModeID;
  338. }
  339.  
  340. GLenum auxGetDisplayMode(void)
  341. {
  342.    return displayModeType;
  343. }
  344.  
  345. void auxSetOneColor(int index, float r, float g, float b)
  346. {
  347.     tkSetOneColor(index, r, g, b);
  348. }
  349.  
  350. void auxSetFogRamp(int density, int startIndex)
  351. {
  352.     tkSetFogRamp(density, startIndex);
  353. }
  354.  
  355. void auxSetGreyRamp(void)
  356. {
  357.     tkSetGreyRamp();
  358. }
  359.  
  360. void auxSetRGBMap(int size, float *rgb)
  361. {
  362.     tkSetRGBMap(size, rgb);
  363. }
  364.  
  365. int auxGetColorMapSize(void)
  366. {
  367.     return tkGetColorMapSize();
  368. }
  369.  
  370. void auxGetMouseLoc(int *x, int *y)
  371. {
  372.     tkGetMouseLoc(x, y);
  373. }
  374.  
  375. AGLPixelFormat auxGetPixelFmt(void)
  376. {
  377.     return tkGetPixelFmt();
  378. }
  379.